package com.bwie.goods.datastructureTwo;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder;

import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class two {
    public static void main(String[] args) {
        // 创建字典对象
        Person person = new Person("张三", 25, new Address("10001", "上海", "奉贤"));
        person.addFriend(new Person("李四", 27, new Address("10002", "上海", "虹桥")));
        person.addFriend(new Person("王五", 24, new Address("20001", "杭州", "西湖")));

        // 使用Gson库将字典对象转换为JSON格式字符串
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonStr = gson.toJson(person);
        try (OutputStreamWriter writer = new OutputStreamWriter(System.out, "UTF-8")) {
            writer.write(jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class Person {
    private String name;
    private int age;
    private Address address;
    private List<Person> friends;

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.friends = new ArrayList<>();
    }

    public void addFriend(Person friend) {
        this.friends.add(friend);
    }
}

class Address {
    private String zip;
    private String city;
    private String state;

    public Address(String zip, String city, String state) {
        this.zip = zip;
        this.city = city;
        this.state = state;
    }
}
